/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.dialog

import android.content.Context
import android.graphics.PixelFormat
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.ecarx.systemui.plugin.AppConstants
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.controller.GPSSController
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog
import com.ecarx.systemui.plugin.utils.DensityUtils
import com.ecarx.systemui.plugin.utils.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class CarLocationDialog: AutoCloseDialog, GPSSController.OnGpsStatusListener {

    companion object {
        private const val TAG = "CarLocationDialog"
    }

    private var mPositionX = 0
    private var mGpsGroup:View? = null
    private var mTvGpsInfo:TextView? = null
    constructor(pos: Int, context: Context) : this(context, 0){
        mPositionX = pos
    }
    constructor(context: Context, themeResId: Int) : super(context, themeResId)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initConfig()
        setContentView(R.layout.dialog_car_location)
        initView()
    }

    private fun initView() {
        mGpsGroup = findViewById<View>(R.id.group_gpsinfo)
        mTvGpsInfo = findViewById<TextView>(R.id.tv_gpsinfo)
    }

    private fun initConfig() {
        val window = window
        if (window != null) {
            val params = WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                AppConstants.PLUGIN_DIALOG_TYPE,
                WindowManager.LayoutParams.FLAG_FULLSCREEN or
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                PixelFormat.TRANSPARENT
            )
            params.gravity = Gravity.START or Gravity.TOP
            params.y = DensityUtils.dp2px(94f)
            params.x = mPositionX
            LogUtils.d(TAG, "params.x= $mPositionX")
            window.attributes = params
            setCanceledOnTouchOutside(true)
        }
    }

    fun updateConfig(location: Int) {
        mPositionX = location
        initConfig()
    }

    override fun show() {
        super.show()
        // 获取 GPS 状态
        showGpsInfo(GPSSController.getInstance(context).currentGpsStatus)
        // 监听 GPS 状态
        GPSSController.getInstance(context).addGpsStatusListener(this)
        // 解注册
        setOnDismissListener {
            GPSSController.getInstance(context).removeGpsStatusListener(this)
        }
    }

    override fun onStatusChange(status: Int) {
        CoroutineScope(Dispatchers.Main).launch {
            showGpsInfo(status)
        }
    }

    /**
     * 获取 Gps 状态从而变换文案，同 GPSView 的获取逻辑
     */
    private fun showGpsInfo(gpsStatus: Int){
        mTvGpsInfo?.text = when(gpsStatus){
            0 -> {
                mGpsGroup?.visibility = View.VISIBLE
                context.resources.getString(R.string.gps_signal_lost)
            }
            1 -> {
                mGpsGroup?.visibility = View.VISIBLE
                context.resources.getString(R.string.gps_signal_weak)
            }
            else -> {
                mGpsGroup?.visibility = View.GONE
                ""
            }
        }
    }
}
